package com.zst.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5相关工具类
 * @author zst
 */
public class MD5Utils {
    public static String encrypt(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            byte[] bytes = md.digest(data.getBytes());
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < bytes.length; i++) {
                //把bytes转换成16禁止的数字，这一步不太懂，从网上copy的
                result.append(Integer.toHexString((0x000000FF & bytes[i]) | 0xFFFFFF00).substring(6));
            }

            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Cannot Found MD5 Algorithm");
        }
    }
}
